package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.OrderDeleteId;
import com.cskaoyan.bean.bo.OrderRefundBO;
import com.cskaoyan.bean.bo.OrderReplyBO;
import com.cskaoyan.bean.bo.OrderShipBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketAdmin;
import com.cskaoyan.bean.pojo.MarketOrder;
import com.cskaoyan.bean.vo.OrderChannelVO;
import com.cskaoyan.bean.vo.OrderDetailVO;
import com.cskaoyan.bean.vo.OrderListVO;
import com.cskaoyan.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.cskaoyan.util.Constant.channelMap;

/**
 * @author rebaogu
 * @description 订单controller层
 * @date 2022.04.06 23:14
 * @package com.cskaoyan.controller
 * @Classname MarketOrderController
 */
@RestController
@RequestMapping("admin/order")
public class MarketOrderController {

    @Autowired
    OrderService orderService;



    // 展示列表
    @RequestMapping("list")
    public BaseRespVo orderList(BaseParam baseParam, Short orderStatusArray, Integer orderId, @DateTimeFormat Date start, @DateTimeFormat Date end, Integer userId, String orderSn) {

        OrderListVO orderListVO = orderService.orderList(baseParam,orderStatusArray,orderId,start,end,userId,orderSn);
        return BaseRespVo.ok(orderListVO);
    }

    @RequestMapping("channel")
    public BaseRespVo orderChannel() {
        List<OrderChannelVO> orderChannelVOList = orderService.orderChannel();
        return BaseRespVo.ok(orderChannelVOList);
    }

    // 详情
    @RequestMapping("detail")
    public BaseRespVo orderDetail(Integer id) {
        OrderDetailVO orderDetailVO = orderService.orderDetail(id);
        return BaseRespVo.ok(orderDetailVO);
    }


    /**
     *
     * @param orderId
     * @return com.cskaoyan.bean.BaseRespVo
     * @author luoxuxin 罗旭欣 添加删除订单的方法（假删）
     * @since 2022/04/11 15:33
     */
    @PostMapping("delete")
    public BaseRespVo orderDelete(@RequestBody OrderDeleteId orderId) {
        orderService.orderDelete(orderId.getOrderId());
        return BaseRespVo.ok();
    }


    // 退款
    @RequestMapping("refund")
    public BaseRespVo orderRefund(@RequestBody  OrderRefundBO orderRefundBO) {
        int code = orderService.orderRefund(orderRefundBO);
        if (code == 200) {
            return BaseRespVo.ok("成功");
        }
        if (code == 500) {
            return BaseRespVo.fail(622, "订单退款失败");
        }
        return BaseRespVo.fail(10000,"服务器繁忙");
    }

    // 发货
    @RequestMapping("ship")
    public BaseRespVo orderShip(@RequestBody OrderShipBO orderShipBO) {
        int code = orderService.orderShip(orderShipBO);
        if (code == 200) {
            return BaseRespVo.ok("成功");
        }
        if (code == 500) {
            return BaseRespVo.fail(622, "订单发货失败");
        }
        return BaseRespVo.fail(10000,"服务器繁忙");
    }

    // 商品评论 --> 回复
    @RequestMapping("reply")
    public BaseRespVo orderReply(@RequestBody  OrderReplyBO orderReplyBO) {
        int code = orderService.orderReply(orderReplyBO);
        if (code == 200) {
            return BaseRespVo.ok("成功");
        }
        if (code == 500) {
            return BaseRespVo.fail(622, "订单商品已经回复");
        }
        return BaseRespVo.fail();
    }

}
